import scrapy
from scrapy.linkextractors import LinkExtractor
from scrapy.spiders import CrawlSpider, Rule
from  ..items import ScrapyReadbookItem


class ReadSpider(CrawlSpider):
    name = "read"
    allowed_domains = ["www.dushu.com"]
    start_urls = ["https://www.dushu.com/book/1006_1.html"]

    rules = (Rule(LinkExtractor(allow=r"/book/1006_\d+\.html"),
                  callback="parse_item",
                  follow=True),)

    def parse_item(self, response):
        print("===============================================")
        img_list = response.xpath('//div[@class="book-info"]//img')
        for img in img_list:
            src=img.xpath('./@data-original').extract_first()
            name=img.xpath('./@alt').extract_first()

        #item["domain_id"] = response.xpath('//input[@id="sid"]/@value').get()
        #item["name"] = response.xpath('//div[@id="name"]').get()
        #item["description"] = response.xpath('//div[@id="description"]').get()
            book = ScrapyReadbookItem(src=src, name=name)
            yield  book
